<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
        $this->display();
    }

    //注册
    public function register() {
        $userName = I('post.userName', '');
        $email = I('post.email', '');
        $password = I('post.password', '');
        if(empty($userName)){
            $message['message'] = '用户名不能为空！';
            $message['type'] = 'userNameError';
            $message['status'] = 0;
        }else if(empty($email)){
            $message['message'] = '邮箱不能为空！';
            $message['type'] = 'emailError';
            $message['status'] = 0;
        }else if(empty($password)) {
            $message['message'] = '密码不能为空！';
            $message['type'] = 'passwordError';
            $message['status'] = 0;
        }else {
            $user = M('user');
            $userInfo = $user->where(array("userName"=>$userName))->find();
            $emailInfo = $user->where(array("email"=>$email))->find();
            if(!empty($userInfo)) {
                $message['message'] = '用户名被占用！';
                $message['type'] = 'userNameError';
                $message['status'] = 0;
            }else if(!empty($emailInfo)) {
                $message['message'] = '邮箱被占用！';
                $message['type'] = 'emailError';
                $message['status'] = 0;
            }else {
                $userInfo['userName'] = $userName;
                $userInfo['password'] = $password;
                $message['type'] = 'passwordError';
                $userInfo['email'] = $email;
                $userInfo['lastip'] = get_client_ip();
                $userInfo['regdate'] = time();
                $userInfo['photo'] = "http://ogrv04tc4.bkt.clouddn.com/2016-11-17_582d48713a980.jpg";
                $user->data($userInfo)->add();
                $message['message'] = '注册成功！';
                $message['status'] = 1;
                $uUser = $user->where(array('userName' =>$userInfo['userName']))->find();
                
                session('photo', $userInfo['photo']);
                session('uid', $uUser["uid"]);
                session('userName', $userInfo["userName"]);
            }
        }
        $this->ajaxReturn($message);
    }

    //登录
    public function login() {
        $userName = I('post.userName', '');
        $password = I('post.password', '');
        if($userName && $password) {
            $User = M('user');
            $result = $User->where(array('userName'=> $userName))->find();
            if($result && $result['password'] == $password) {                
                $message['message'] = '登录成功！';
                $message['status'] = 1;
                
                session('userName', $userName);
                session('photo', $result["photo"]);
                session('uid', $result["uid"]);
            }else {
                $message['message'] = '用户名或密码错误！';
                $message['type'] = 'passwordError';
                $message['status'] = 0;
            }
        }else if(!$userName) {
            $message['message'] = '用户名不能为空！';
            $message['type'] = 'userNameError';
            $message['status'] = 0;
        }else if(!$password) {
            $message['message'] = '密码不能为空！';
            $message['type'] = 'passwordError';
            $message['status'] = 0;
        }
        $this->ajaxReturn($message);
    }

    public function CheckLogin() {
        $this->ajaxReturn(session('uid'));
    }

    //注销登录
    public function logout() {
        session('userName', null);
        session('uid', null);
        session('photo', null);
        $this->redirect('/');
    }
}


